package com.scm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.CheckStockMapper;
import com.scm.dao.PoitemMapper;
import com.scm.dao.PomainMapper;
import com.scm.dao.ProductMapper;
import com.scm.dao.SoitemMapper;
import com.scm.dao.SomainMapper;
import com.scm.dao.StockRecordMapper;
import com.scm.entity.CheckStock;
import com.scm.entity.Poitem;
import com.scm.entity.Soitem;
import com.scm.entity.StockRecord;
import com.scm.exception.TransactionException;
import com.scm.service.IStockService;
import com.scm.util.DateUtil;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StockServiceImpl
        implements IStockService
{
    @Autowired
    private PomainMapper pomainMapper;
    @Autowired
    private SomainMapper somainMapper;
    @Autowired
    private PoitemMapper poitemMapper;
    @Autowired
    private SoitemMapper soitemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StockRecordMapper stockRecordMapper;
    @Autowired
    private CheckStockMapper checkStockMapper;

    @Transactional
    public void instock(long poid, String account)
    {
        String now = DateUtil.currentTime();
        HashMap<String, Object> map = new HashMap();
        map.put("now", now);
        map.put("poId", Long.valueOf(poid));
        map.put("account", account);

        int n = this.pomainMapper.instock(map);
        if (n != 1) {
            throw new TransactionException("修改采购单主单入库信息失败");
        }
        List<Poitem> poitems = this.poitemMapper.selectByPoId(poid);

        n = this.productMapper.instock(poitems);
        if (n <= 0) {
            throw new TransactionException("修改采购在途及增加库存数失败");
        }
        HashMap<String, Object> m = new HashMap();
        m.put("items", poitems);
        m.put("stockTime", now);
        m.put("stockType", Integer.valueOf(1));
        m.put("account", account);
        m.put("id", Long.valueOf(poid));
        this.stockRecordMapper.insert(m);
    }

    @Transactional
    public void outstock(long soid, String account)
    {
        String now = DateUtil.currentTime();
        HashMap<String, Object> map = new HashMap();
        map.put("now", now);
        map.put("soId", Long.valueOf(soid));
        map.put("account", account);

        int n = this.somainMapper.outstock(map);
        if (n != 1) {
            throw new TransactionException("修改销售单主单出库信息失败");
        }
        List<Soitem> soitems = this.soitemMapper.selectBySoId(soid);
        if ((soitems == null) || (soitems.size() == 0)) {
            throw new TransactionException("没有销售单的产品明细，不能为出库");
        }
        n = this.productMapper.outstock(soitems);
        if (n <= 0) {
            throw new TransactionException("库存不足");
        }
        HashMap<String, Object> m = new HashMap();
        m.put("items", soitems);
        m.put("stockTime", now);
        m.put("stockType", Integer.valueOf(2));
        m.put("account", account);
        m.put("id", Long.valueOf(soid));
        this.stockRecordMapper.insert(m);
    }

    @Transactional
    public void checkStock(CheckStock cs, int num)
    {
        this.checkStockMapper.insert(cs);

        HashMap<String, Object> m = new HashMap();
        m.put("productCode", cs.getProductCode());
        m.put("num", Integer.valueOf(num));
        this.productMapper.check(m);

        StockRecord record = new StockRecord();
        record.setCreateUser(cs.getCreateUser());
        record.setProductCode(cs.getProductCode());
        record.setStockNum(Integer.valueOf(Math.abs(num)));
        record.setStockTime(cs.getStockTime());
        record.setStockType(Integer.valueOf(num > 0 ? 3 : 4));
        this.stockRecordMapper.insertCheck(record);
    }

    public PageInfo<HashMap<String, Object>> selectReport(Integer page, String time)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<HashMap<String, Object>> list = this.stockRecordMapper.selectProductStockRecord(time);
        PageInfo<HashMap<String, Object>> info = new PageInfo(list);
        return info;
    }

    public PageInfo<HashMap<String, Object>> selectInstock(Integer page, String time)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<HashMap<String, Object>> pomains = this.stockRecordMapper.selectInstockPomain(time);
        PageInfo<HashMap<String, Object>> info = new PageInfo(pomains);
        return info;
    }

    public PageInfo<HashMap<String, Object>> selectOutstock(Integer page, String time)
    {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        List<HashMap<String, Object>> somains = this.stockRecordMapper.selectOutstockPomain(time);
        PageInfo<HashMap<String, Object>> info = new PageInfo(somains);
        return info;
    }
}
